#include "CheckBox.h"
namespace NBGUI
{
	CheckBox::CheckBox(GUIDrawer* drawer, const char* name, const char* eleText, int eleType, Rect& _rect ):
ElementNode(drawer, name,eleText,eleType,_rect),state(UnChecked),Grid3x3(2,2,64,64)
{
	//this is the uc of the tex in default png
	uv[0] = vec2f(0,64.0f/512);
	uv[1] = vec2f(0,128.0f/512);
	uv[2] = vec2f(64.0f/512,128.0f/512);
	uv[3] = vec2f(64.0f/512,64.0f/512);

	uv[4] = vec2f(64.0f/512,64.0f/512);
	uv[5] = vec2f(64.0f/512,128.0f/512);
	uv[6] = vec2f(128.0f/512,128.0f/512);
	uv[7] = vec2f(128.0f/512,64.0f/512);

	textDrawingPack = drawer->font->addText(elementText.c_str(),(float)_rect.x + _rect.w + 10,(float)_rect.y + _rect.h ,10);

}

CheckBox::CheckBox( GUIDrawer* drawer, const char* name, const char* eleText,vec2f* _uv, int eleType /*= NONE*/, Rect& _rect /*= Rect()*/ ):
ElementNode(drawer, name,eleText,eleType,_rect),state(UnChecked),Grid3x3(2,2,64,64)
{
	memcpy_s(uv,sizeof(vec2f)*4,_uv,sizeof(vec2f)*4);
}

CheckBox::~CheckBox()
{
}

void CheckBox::draw()
{
	if(hide)
		return;
	//draw something based on the element type
	//draw the children
	//need to load texture to draw
	//need to know the x y and size of the element
	pDrawer->pRenderer->applyTexture(0);
	if (state == Checked)
	{
		pDrawer->pRenderer->drawIndexed(startIndex,indexLen,pDrawer->vboId,pDrawer->iboId);
		pDrawer->font->drawStrPack(textDrawingPack);
	}
	else
	{
		pDrawer->pRenderer->drawIndexed(startIndex,indexLen/2,pDrawer->vboId,pDrawer->iboId);
		pDrawer->font->drawStrPack(textDrawingPack);
	}



	for(auto it = children.begin(); it != children.end(); ++it)
	{
		(*it)->draw();
	}
}

void CheckBox::buildVI( std::vector<Vertex>* vertices, std::vector<unsigned int>* indices,unsigned int screenWidth, unsigned int screenHeight )
{
	startIndex = indices->size();

	for (int checkValue = 0; checkValue< StateNum; ++checkValue)
	{
		for (int i = 0; i < 9; ++i)
		{
			Rect gridRect = grid[i];
			float left, right, top, bottom;



			if(i == TOPRIGHT || i == MIDRIGHT || i == BOTTOMRIGHT)
			{
				left = (-(float)screenWidth / 2) + (float)rect.x + rect.w - edgeWidth;
			}
			else
			{
				left = (-(float)screenWidth / 2) + (float)rect.x + (float)gridRect.x ;
			}



			if(i == TOPMID || i == MIDMID || i == BOTTOMMID)
			{
				//mid grid , draw as the xml says
				right = left + rect.w - 2 * edgeWidth;
			}
			else
			{
				right = left + (float)gridRect.w ;
			}

			if (i > MIDRIGHT && i <= BOTTOMRIGHT)
			{
				//bottom 3 grid
				top = (float)screenHeight / 2 - (float)rect.y - (rect.h - edgeHeight);
			}
			else
			{
				top = (float)screenHeight / 2 - (float)rect.y -  (float)gridRect.y;
			}

			if(i > TOPRIGHT && i <= MIDRIGHT)
			{
				bottom = top - (rect.h - 2 * edgeHeight) ;
			}
			else
			{
				bottom = top - (float)gridRect.h ;
			}

			{
				Vertex eleVertices[4];
				unsigned int currentVertexNum = vertices->size();
				unsigned int eleIndices[6]={0,1,2,0,2,3};


				eleVertices[0].pos = vec3f(left, top, 0.0f);  // Top left.
				eleVertices[0].coord = uv[4*checkValue+0];
				eleVertices[0].coord.x += gridRect.x/512.0f;
				eleVertices[0].coord.y += gridRect.y/512.0f;

				eleVertices[1].pos = vec3f(left, bottom, 0.0f);  // Bottom left.
				eleVertices[1].coord = eleVertices[0].coord;
				eleVertices[1].coord.y += gridRect.h/512.0f;

				eleVertices[2].pos = vec3f(right, bottom, 0.0f);  // Bottom right.
				eleVertices[2].coord = eleVertices[0].coord;
				eleVertices[2].coord.x += gridRect.w/512.0f;
				eleVertices[2].coord.y += gridRect.h/512.0f;

				eleVertices[3].pos = vec3f(right, top, 0.0f);  // Top right.
				eleVertices[3].coord = eleVertices[0].coord;
				eleVertices[3].coord.x += gridRect.w/512.0f;

				for (int i = 0; i < 4; ++i)
				{
					vertices->push_back(eleVertices[i]);
				}



				for (int i = 0; i  < 6; ++i)
				{
					indices->push_back(currentVertexNum + eleIndices[i]);
				}

				indexLen += 6;
			}


		}
	}

	

}

void CheckBox::handleEvent( Event* e )
{
	switch(e->type)
	{
	case MOUSE_BUTTON_PRESS:

		if( !isInside(e) )
		{
			return;
		}

		state = state == UnChecked? Checked : UnChecked;

		for (size_t i = 0; i< handlerList.size();++i)
		{
			ElementNode* start = pDrawer->playout->root;
			ElementNode* nd = findNode(start,handlerList[i].name);
			if(nd && state == handlerList[i].eventType)
				nd->doSomething( handlerList[i].parameters, handlerList[i].funcName);
		}

		


		break;
	}
}

}